/**
 * Copyright 2006 Bosco Curtu
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. 
 * 
 * See the License for the specific language governing permissions and limitations 
 * under the License.
*/

package com.bcurtu.james;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.log4j.Logger;

public class TelnetFactory implements PoolableObjectFactory {

	private Logger log=Logger.getLogger(this.getClass());
	
	private String ip;
	private int port;
	private String user;
	private String pwd;
	private String step1;
	private String step2;

	public String getStep1() {
		return step1;
	}

	public void setStep1(String step1) {
		this.step1 = step1;
	}

	public String getStep2() {
		return step2;
	}

	public void setStep2(String step2) {
		this.step2 = step2;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public Object makeObject() throws Exception {
//		log.debug("makeObject");
		Socket s=new Socket();
		openSession(s);
		return s;
	}

	public void destroyObject(Object arg0) throws Exception {
//		log.debug("destroyObject");
		Socket s=(Socket)arg0;
		if (s.isConnected()) s.close();
		s=null;
	}

	public boolean validateObject(Object arg0) {
//		log.debug("validateObject");
		Socket s=(Socket)arg0;
		if (!s.isConnected()) return false;
		return validaSocket(s);
	}

	private boolean validaSocket(Socket s){
		try{
			OutputStream os=s.getOutputStream();
			os.write("\n".getBytes());
			InputStream is=s.getInputStream();
			BufferedReader bf=new BufferedReader(new InputStreamReader(is));
			bf.readLine();
			return true;
		} catch(Exception ex){
			return false;
		}
		
	}
	
	public void activateObject(Object arg0) throws Exception {
//		log.debug("activateObject");
		Socket s=(Socket)arg0;
		if (s.isClosed()){
			openSession(s);
		}
	}

	public void passivateObject(Object arg0) throws Exception {
//		log.debug("passivateObject");
		// TODO Auto-generated method stub

	}
	
	private void openSession(Socket s) throws Exception{
//		log.debug("openSession");
		s.connect(new InetSocketAddress(ip,port));
		InputStream is=s.getInputStream();
		OutputStream os=s.getOutputStream();
		
		BufferedReader bf=new BufferedReader(new InputStreamReader(is));
		String line=bf.readLine();

		while(!(step1.equals(line))){
			line=bf.readLine();
		}
		String cmd=user+"\n";
		os.write(cmd.getBytes());
		
		
		while(!(step2.equals(line))){
			line=bf.readLine();
		}
		cmd=pwd+"\n";
		os.write(cmd.getBytes());
		line=bf.readLine();
		
	}

}
